package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentsMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Pagination;
import com.itheima.pojo.Student;
import com.itheima.service.StudentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.Subject;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class StudentsImpl implements StudentsService {
    @Autowired
    private StudentsMapper studentsMapper;


    /**
     * 查询所有
     * @param clazzPage
     * @return
     */
    @Override
    public PageResult list(Pagination clazzPage) {
        //开启分页
        PageHelper.startPage(clazzPage.getPage(),clazzPage.getPageSize());
        //查询数据
        List<Student> list = studentsMapper.lists(clazzPage);
        //封装分页信息
        Page<Student> page = (Page<Student>) list;
        return new PageResult<Student>(page.getTotal(),page.getResult());
    }



    /**
     * 删除
     * @param ids
     */
    @Override
    public void dept(List<Integer> ids) {
        studentsMapper.deptStudents(ids);
    }



    /**
     * 添加
     * @param student
     */
    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentsMapper.add(student);
    }

    /**
     * 查询
     * @param id
     */
    @Override
    public Student listid(Integer id) {
        return studentsMapper.listid(id);
    }

    /**
     * 修改学员
     * @param student
     */
    @Override
    public void udete(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentsMapper.undate(student);
    }

    /**
     * 修改学员的违规次数和违规分数
     * @param id
     * @param score
     */
    @Transactional
    @Override
    public void undatevs(Integer id, Short score) {
        studentsMapper.undatevos(id,score);
       Student student =studentsMapper.listid(id);
       //违规次数
       student.setViolationCount((short)+1);
    }
}
